package com.yuexunit.operationman.nbsy.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yuexunit.operationman.nbsy.R;

/**
 * Created by work-jx on 2017/7/27.
 */
public class SetHRDialog {
    private Context context;
    private Dialog dialog;
    private Display display;

    TextView time;
    EditText person_num;
//    EditText person_psw;
    Button sure;
    Button cancel;

    /**
     * 一。内部接口，要求实现一个callback方法
     * @author Administrator cuiweiyou.com
     */
    private ICallBack icb;
//    private TextView tv;

    public SetHRDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public SetHRDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_hr, null);

        time = (TextView)view.findViewById(R.id.time);
        person_num = (EditText)view.findViewById(R.id.person_num);
//        person_psw = (EditText)view.findViewById(R.id.person_psw);
        cancel = (Button)view.findViewById(R.id.cancel);
        sure = (Button)view.findViewById(R.id.sure);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(icb != null){
                    icb.callback(person_num.getText().toString());
                }
            }
        });

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.loadingDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    /**
     * 三。设置回调变量
     * @param cb
     */
    // 如果不是使用上面接收icallback参数的(二)构造方法创建此自定义对话框，则需要使用此方法指定icb变量
    public void setCallBack (ICallBack cb) {
//        this.tv = tv;
        icb = cb;
    }

    public SetHRDialog setTxt(String time) {
        this.time.setText(time);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    public interface ICallBack {
        void callback(String date);
    }
}
